package com.example.elspring;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

@SpringBootApplication
@PropertySource("classpath:test.properties")
public class ElSpringApplication implements ApplicationRunner {

    //注入普通字符串
    @Value("注入普通字符串")
    private String normal;

    //注入操作系统属性
    @Value("#{systemProperties['os.name']}")
    private String osName;

    //注入表达式结果
    @Value("#{T(java.lang.Math).random() * 100.0}")
    private double randomNumber;

    //注入其他Bean属性
    @Value("#{demoService.another}")
    private String fromAnother;

    //注入文件资源
    @Value("classpath:test.txt")
    private Resource testFile;

    //注入网址资源
    @Value("http://www.baidu.com")
    private Resource testUrl;

    //注入配置文件中的属性
    @Value("${book.name}")
    private String bookName;

    //注入配置Bean对象
    @Autowired
    private BookConf bookConf;

    //注入spring的属性变量接口
    @Autowired
    private Environment environment;

    public static void main(String[] args) {
        SpringApplication.run(ElSpringApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(normal);
        System.out.println(osName);
        System.out.println(randomNumber);
        System.out.println(fromAnother);
        System.out.println(IOUtils.toString(testFile.getInputStream(), "utf-8"));
        System.out.println(IOUtils.toString(testUrl.getInputStream(), "utf-8"));
        System.out.println(bookName);
        System.out.println(bookConf.getName());
        System.out.println(environment.getProperty("book.author"));
    }
}
